package cool.mtc.minghe.web.controller.sys.org;

import cool.mtc.core.page.PageData;
import cool.mtc.minghe.data.model.base.IdForm;
import cool.mtc.minghe.data.model.sys.org.dto.OrgDTO;
import cool.mtc.minghe.data.model.sys.org.dto.OrgDictDTO;
import cool.mtc.minghe.data.model.sys.org.dto.OrgListDTO;
import cool.mtc.minghe.data.model.sys.org.form.OrgForm;
import cool.mtc.minghe.data.model.sys.org.query.OrgQuery;
import cool.mtc.minghe.service.service.sys.org.OrgService;
import cool.mtc.minghe.web.controller.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 明河
 */
@Tag(name = "单位管理")
@RestController
@RequestMapping("api/sys/org")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class OrgController extends BaseController {
    private final OrgService orgService;

    @PreAuthorize("@ps.hasPermission('sys:org:insert')")
    @Tag(name = "单位管理")
    @Operation(summary = "新增", description = "权限：sys:org:insert")
    @PostMapping("insert")
    public long insert(@RequestBody @Validated OrgForm form) {
        return orgService.insert(form);
    }

    @PreAuthorize("@ps.hasPermission('sys:org:update')")
    @Tag(name = "单位管理")
    @Operation(summary = "更新", description = "权限：sys:org:update")
    @PostMapping("update")
    public void update(@RequestBody @Validated OrgForm form) {
        orgService.update(form);
    }

    @PreAuthorize("@ps.hasPermission('sys:org:delete')")
    @Tag(name = "单位管理")
    @Operation(summary = "删除", description = "权限：sys:org:delete")
    @PostMapping("delete")
    public void delete(@RequestBody IdForm form) {
        orgService.delete(form.getId());
    }

    @PreAuthorize("@ps.hasPermission('sys:org:read')")
    @Tag(name = "单位管理")
    @Operation(summary = "列表", description = "权限：sys:org:read")
    @GetMapping("list")
    public List<OrgListDTO> list(OrgQuery query) {
        return orgService.list(query);
    }

    @PreAuthorize("@ps.hasPermission('sys:org:read')")
    @Tag(name = "单位管理")
    @Operation(summary = "分页列表", description = "权限：sys:org:read")
    @GetMapping("list/page")
    public PageData<OrgListDTO> pageList(OrgQuery query) {
        return orgService.pageList(query);
    }

    @PreAuthorize("@ps.hasPermission('sys:org:read')")
    @Tag(name = "单位管理")
    @Operation(summary = "详情", description = "权限：sys:org:read")
    @GetMapping("info")
    public OrgDTO info(Long id) {
        return orgService.info(id);
    }

    @PreAuthorize("@ps.hasPermission('sys:org:read')")
    @Tag(name = "单位管理")
    @Operation(summary = "字典树", description = "权限：sys:org:read")
    @GetMapping("dict/tree")
    public List<OrgDictDTO> listDictTree() {
        return orgService.listDictTree();
    }
}
